# -*- coding: utf-8 -*-

from gtkmvc import Controller
from gtkmvc.adapters import UserClassAdapter
import os

class PyvrCtrl (Controller):
    """Handles signal processing, and keeps alignment of model and view"""
    def __init__(self, model):
        Controller.__init__(self, model)
        return

    def register_view(self, view):

        Controller.register_view(self, view)

        # fill combo ...
        self.view.add_lang(self.model.get_lang(), self.on_ck_lang_toggled)
        self.view.set_quality_list(self.model.get_quality_list())
        self.view.set_video_codec_list(self.model.get_video_codec_list())
        self.view.set_audio_codec_list(self.model.get_audio_codec_list())

        # fill file list ...
        self.view.set_file_list(self.model.get_file_list())
        return

    def register_adapters(self):
        # view reflect model value et vice et versa
        self.adapt("new_filename")
        self.adapt('mask_length')
        return
    
    # gtk signals
    def on_bt_open_clicked(self, buton):

        ## mini MVC for choose file dialog...
        from view import PyvrChooseFolderView
        c = PyvrChooseFolderCtl(self.model)
        v = PyvrChooseFolderView(c, parent_view=self.view)
        v.run()
        self.model.unregister_observer(c)
        return True
    
    def on_cb_files_changed(self, cb):
        entry = cb.get_active_text()
        if entry == None: return

        self.model.file = entry
    
    def on_ck_lang_toggled(self, ck, data):
        self.model.lang_toggle(data['lang'], data['type'], ck.get_active())
        self.model.update_new_filename()

    def on_ck_nfo_toggled(self, ck):
        self.model.set_nfo_create(ck.get_active())
        
    def on_cb_video_codec_changed(self, cb):
        entry = cb.get_active_text()
        if entry == None: return
        self.model.vcodec = entry
    
    def on_cb_audio_codec_changed(self, cb):
        entry = cb.get_active_text()
        if entry == None: return
        self.model.acodec = entry

    def on_cb_quality_changed(self, cb):
        entry = cb.get_active_text()
        if entry == None: return
        self.model.quality = entry

    def on_bt_quit_clicked(self, buton):
        import gtk
        gtk.main_quit()
        return
    
    def on_bt_play_clicked(self, bt):
        """
        callback qui lance le film
        """
        if(os.name == 'nt'):
            os.startfile(os.path.join(self.model.directory, self.model.file))
        else:
            os.system('xdg-open "' + os.path.join(self.model.directory, self.model.file)+'"')
        
        return
    
    def on_bt_cancel_clicked(self, buton):
        self.property_file_value_change(self.model, '', self.model.file)
        return
    # observable properties
    
    def on_bt_apply_clicked(self, buton):
        self.model.save()
        # reload file list
        
        file = self.model.new_filename
        self.property_directory_value_change(self.model, '', '')
        self.view.select_file(file)
        return
    
    def on_bt_apply_and_next_clicked(self, buton):
        self.on_bt_apply_clicked(buton)
        self.view.select_next_file()
    
    def property_directory_value_change(self, model, old, new):
        self.view.set_file_list(model.get_file_list())
        return
   
    def property_file_value_change(self, model, old, new):
        # adjust also the spin initial value
        self.view['mask_length'].get_adjustment().value = model.guess_mask_length()
        model.analyse_film()
        model.find_nfo(new)
        self.view.set_title(new)
        return
   
    def property_film_value_change(self, model, old, new):
        f = model.film
        self.view.set_film_specs(f.getVideoCodec(),
                                 f.getVideoResolution(),
                                 f.getVideoQuality(),
                                 f.getAudioCodec(),
                                 f.getAudioNbTracks(),
                                 f.getLang(),
                                 f.getSubLang())

    def property_vcodec_value_change(self, model, old, new):
        model.update_new_filename()
        return

    def property_acodec_value_change(self, model, old, new):
        model.update_new_filename()
        return
        
    def property_quality_value_change(self, model, old, new):
        model.update_new_filename()
        return

    def property_mask_length_value_change(self, model, old, new):
        model.update_new_filename()
        return

    def property_nfo_value_change(self, model, old, new):
        """ methode invoquée lorsque la propriété nfo à changé """
        self.view.set_nfo_presence(new != None)
        return

    pass # end of class


class PyvrChooseFolderCtl(Controller):
    
    def on_w_open_folder_response(self, dialog, response):
        
        import gtk
        if response == gtk.RESPONSE_OK:
            self.model.directory = dialog.get_filename()
#        elif response == gtk.RESPONSE_CANCEL:
#            print 'Closed, no files selected'
    pass